﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SignInServices.Code;

namespace SignInServices.Admin
{
    public partial class RegisterNewUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void cuwUser_CreatingUser(object sender, LoginCancelEventArgs e)
        {
            
        }

        protected void btnCreateUser_Click(object sender, EventArgs e)
        {
            try
            {
                lblMessages.Text = "";
                doCreateNewUser(txtFirstName.Text, txtLastName.Text, txtEmail.Text,
                                txtPassword.Text, txtConfirmPassword.Text, txtUserName.Text);
                lblMessages.Text = " User created successfully";
                clearControls();
            }
            catch (Exception ex)
            {
                lblMessages.Text = ex.Message;
            }
            
        }

        private void clearControls()
        {
            txtFirstName.Text = "";
            txtLastName.Text = "";
            txtEmail.Text = "";
            txtPassword.Text = "";
            txtFirstName.Focus();
        }

        private void doCreateNewUser(string firstNAme, string lastName, 
            string eMail, string password, string confirmPassword, string userName)
        {
            try
            {
                if (password != confirmPassword)
                    throw new Exception("Two given passwords do not match.");
                if (firstNAme.Trim() == string.Empty)
                    throw new Exception("First Name not specified.");
                if (lastName.Trim() == string.Empty)
                    throw new Exception("Last Name not specified.");
                if (eMail.Trim() == string.Empty)
                    throw new Exception("Email Address not specified.");
                if (password.Trim() == string.Empty)
                    throw new Exception("Password not specified.");
                if (confirmPassword.Trim() == string.Empty)
                    throw new Exception("Password confirmation not specified.");

                if (txtUserName.Text.Trim() == string.Empty)
                    throw new Exception("User Name not specified.");

                SSOFederationHelper.FederationObject.CreateUser(userName,password, eMail, true);
                
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}